<?php

namespace Mouta\AmistateBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Mouta\AmistateBundle\Entity\Image
 *
 * @ORM\Entity
 * @ORM\Table(name="images")
 * @ORM\Entity(repositoryClass="Mouta\AmistateBundle\Entity\ImagesRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    protected $path;
    
    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $nombre;

    /**
     * @ORM\ManyToOne(targetEntity="Usuario", inversedBy="images")
     * @ORM\JoinColumn(name="usuario_id", referencedColumnName="id", onDelete="Cascade")
     */
    protected $usuario;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $principal = false;
    
    /**
     * @Assert\File(maxSize="6000000")
     * @Assert\NotNull()
     */
    private $file;
    
    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
    
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);

        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            @unlink($file);
        }
    }
    
    
    public function getAbsolutePath()
    {
        return $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
		if($this->path)
		{
			return '/'.$this->getUploadDir().'/'.$this->path;
		} else {
			if($this->getUsuario())
			{
				if($this->getUsuario()->getSexo())
				{//mujer
					return '/files/images/mujer-sin-foto.png';
				} else {
					return '/files/images/hombre-sin-foto.png';
				}
			} else {
				return '/files/images/mujer-sin-foto.png';
			}
			
		}
        
    }
    
    public function __toString() 
    {
        return $this->getWebPath();
    }
	

    public function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    public function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        $folder = ceil($this->getUsuario()->getId() / 1000);
        return 'files/images/'.$folder;
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return Image
     */
    public function setPath($path)
    {
        $this->path = $path;
    
        return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set principal
     *
     * @param boolean $principal
     * @return Image
     */
    public function setPrincipal($principal)
    {
        $this->principal = $principal;
    
        return $this;
    }

    /**
     * Get principal
     *
     * @return boolean 
     */
    public function getPrincipal()
    {
        return $this->principal;
    }

    /**
     * Set usuario
     *
     * @param \Mouta\AmistateBundle\Entity\Usuario $usuario
     * @return Image
     */
    public function setUsuario(\Mouta\AmistateBundle\Entity\Usuario $usuario = null)
    {
        $this->usuario = $usuario;
    
        return $this;
    }

    /**
     * Get usuario
     *
     * @return \Mouta\AmistateBundle\Entity\Usuario 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Image
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;
    
        return $this;
    }

    /**
     * Get nombre
     *
     * @return string 
     */
    public function getNombre()
    {
        return $this->nombre;
    }
}
